package com.orange.notificationsummary;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.orange.notificationsummary.utils.Constants;
import com.orange.notificationsummary.utils.LogHelper;
import com.orange.notificationsummary.utils.NtfcUtils;

public class NotificationSettingActivity extends Activity {
	private static final String	TAG				= "NotificationSettingActivity";

	private ListView			mSettingEvent;
	private Button				mLockScreen;
	private Button				mDone;

	private boolean				mIsSetting;
	private List<Boolean>		mSettingsArray	= new ArrayList<Boolean>();
	private int					mSettingsCount;

	private List<String>		mListTextArray	= new ArrayList<String>();
	private EventListAdapter	mEventListAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notification_setting_layout);

		mSettingsCount = getResources()
				.getStringArray(R.array.SettingEventList).length;

		final SharedPreferences notificationSettings = getSharedPreferences(
				NotificationGlobal.NOTIFICATION_SETTING, 0);

		for (int i = 0; i < mSettingsCount; i++) {
			mSettingsArray.add(notificationSettings.getBoolean(
					String.valueOf(i), true));
		}
		filldata();
		init();

		mSettingEvent.setOnItemClickListener(new OnItemClickListener() {

			// @Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				mIsSetting = (Boolean) mSettingsArray.get(arg2);
				if (mIsSetting) {
					mSettingsArray.set(arg2, false);
				} else {
					mSettingsArray.set(arg2, true);
				}
				mEventListAdapter.notifyDataSetChanged();
				// updateView();
			}
		});
		mLockScreen.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					ComponentName cn = new ComponentName(
							Constants.PKG_LOCKSCREEN_SETTING,
							"com.orange.lockscreensettings.PreferenceSettingActivity");
					Intent i = new Intent();
					i.setComponent(cn);
					i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					NtfcUtils
							.startActivity(NotificationSettingActivity.this, i);
				} catch (ActivityNotFoundException anfe) {
					LogHelper.e(TAG, "onClick. ", anfe);
				}
			}

		});
		mDone.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				Editor editor = notificationSettings.edit();
				for (int i = 0; i < mSettingsCount; i++) {
					// notificationSettings
					// .edit()
					// .putBoolean(String.valueOf(i),
					// (Boolean) SettingsArray.get(i)).commit();
					editor.putBoolean(Integer.toString(i),
							(Boolean) mSettingsArray.get(i));
				}
				editor.commit();

				Intent i = new Intent(
						NotificationGlobal.UPDATE_SETTING_RECEIVER);
				sendBroadcast(i);
				NotificationSettingActivity.this.finish();
			}

		});
	}

	private void init() {
		mSettingEvent = (ListView) findViewById(R.id.List_event);
		mLockScreen = (Button) findViewById(R.id.lock_screen);
		mDone = (Button) findViewById(R.id.done);
		mLockScreen
				.setVisibility(NtfcUtils.isLockScreenInstalled(this) ? View.VISIBLE : View.INVISIBLE);
		mEventListAdapter = new EventListAdapter(this, mListTextArray,
				mSettingsArray);
		mSettingEvent.setAdapter(mEventListAdapter);
	}

	private void filldata() {
		mListTextArray.clear();
		for (int i = 0; i < mSettingsCount; i++) {
			mListTextArray.add(getResources().getStringArray(
					R.array.SettingEventList)[i]);
		}
	}

}
